package com.fare.taluo.queue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import jxl.write.WritableWorkbook;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.fare.taluo.io.excel.ExcelExportClassicStyle;
import com.fare.taluo.vo.crawl.statistics.StaticHistoryHbaseVO;
import com.fare.util.ReportProperties;
import com.google.common.collect.Maps;

public class ExcelNoFlightInfo extends ExcelExportClassicStyle {

	
	public static void noInfoExcel(Map<String,List<StaticHistoryHbaseVO>> map,String title,String strFilePath){
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 设置单元格类型
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平布局：居中
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
		cellStyle.setWrapText(true);
		
		// 设置字体
		HSSFFont font = workbook.createFont();
		font.setFontName("微软雅黑");
		font.setFontHeightInPoints((short) 11);// 设置字体大小
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗字体
		
		
		for(Entry<String, List<StaticHistoryHbaseVO>> mapE:map.entrySet()){
			String channelId=mapE.getKey();
			List<StaticHistoryHbaseVO> listVo=mapE.getValue();
			// 生成一个表格
			HSSFSheet sheet = workbook.createSheet(channelId);
			sheet.setColumnWidth(0, 100 * 32);
			sheet.setColumnWidth(1, 100 * 32);
			sheet.setColumnWidth(2, 100 * 32);
			sheet.setColumnWidth(3, 100 * 32);
			sheet.setColumnWidth(4, 100 * 32);
			//设置首行信息
			cellStyle.setFont(font);
			
			HSSFRow row_0= sheet.createRow((short) 0);
			HSSFCell cell = row_0.createCell((short) 0);
			cell.setCellStyle(cellStyle);
			cell.setCellValue("出发城市");
			
			HSSFCell cell_0_1 = row_0.createCell((short) 1);
			cell_0_1.setCellStyle(cellStyle);
			cell_0_1.setCellValue("出发城市三字码");
			
			
			HSSFCell cell_0_2 = row_0.createCell((short) 2);
			cell_0_2.setCellStyle(cellStyle);
			cell_0_2.setCellValue("到达城市");
			
			HSSFCell cell_0_3 = row_0.createCell((short) 3);
			cell_0_3.setCellStyle(cellStyle);
			cell_0_3.setCellValue("到达城市三字码");
			
			HSSFCell cell_0_4 = row_0.createCell((short) 4);
			cell_0_4.setCellStyle(cellStyle);
			cell_0_4.setCellValue("出发日期");
			
			//填入数据
			for(int i=0;i<listVo.size();i++){
				StaticHistoryHbaseVO vo=listVo.get(i);
				HSSFRow row = sheet.createRow((short) i+1);//创建行
				
				HSSFCell cell_2 = row.createCell((short) 0);
				cell_2.setCellValue(vo.getFromCity());
				
				HSSFCell cell_3 = row.createCell((short) 1);
				cell_3.setCellValue(vo.getFromCityName());
				
				HSSFCell cell_4 = row.createCell((short) 2);
				cell_4.setCellValue(vo.getToCity());
				
				HSSFCell cell_5 = row.createCell((short) 3);
				cell_5.setCellValue(vo.getToCityName());
				
				HSSFCell cell_6 = row.createCell((short) 4);
				cell_6.setCellValue(vo.getFlightDate());
				
			}
			
			
		}
		
		
		try {
			// 存盘test
//			String strFilePath="C:/";
		//	String strFilePath = ReportProperties.getInstance().getSavePath().endsWith("/")? ReportProperties.getInstance().getSavePath() :	ReportProperties.getInstance().getSavePath()+"/";
			if (!(new File(strFilePath).isDirectory())) {
				new File(strFilePath).mkdirs();
			}
			strFilePath = strFilePath + title+".xls";
//			strFilePath = "c:\\test.xls";
			File fTarget = new File(strFilePath);
			System.out.println("路径："+strFilePath);
			FileOutputStream fileOut = new FileOutputStream(fTarget);
			try {
				workbook.write(fileOut);
				fileOut.close();
//				org.mortbay.log.Log.info("SUCCESS");
				System.out.println("SUCCESS");
			} catch (IOException e) {
				e.printStackTrace();
				e.getMessage();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
	}
	
	@Override
	protected void fillSheet(WritableWorkbook workBook) throws Exception {
		// TODO Auto-generated method stub
	}

	public static void main(String[] args) {
		Map<String,List<StaticHistoryHbaseVO>> map=Maps.newHashMap();
		List<StaticHistoryHbaseVO> l1=new ArrayList<StaticHistoryHbaseVO>();
		List<StaticHistoryHbaseVO> l2=new ArrayList<StaticHistoryHbaseVO>();
		List<StaticHistoryHbaseVO> l3=new ArrayList<StaticHistoryHbaseVO>();
		List<StaticHistoryHbaseVO> l4=new ArrayList<StaticHistoryHbaseVO>();
		StaticHistoryHbaseVO vo=new StaticHistoryHbaseVO();
		vo.setGrabChannelId("1");
		vo.setGrabChannel("test_航空公司_1");
		vo.setFlightDate("2014-10-15");
		vo.setFromCity("PEK");
		vo.setFromCityName("北京");
		vo.setTaskId("afd-dks-ds");
		vo.setToCity("CAN");
		vo.setToCityName("广州");
		l1.add(vo);
		l2.add(vo);
		l3.add(vo);
		l4.add(vo);
		
		StaticHistoryHbaseVO vo2=new StaticHistoryHbaseVO();
		vo2.setGrabChannelId("2");
		vo2.setGrabChannel("test2_航空公司");
		vo2.setFlightDate("2014-10-18");
		vo2.setFromCity("PEK");
		vo2.setFromCityName("北京");
		vo2.setTaskId("afd-dks-ds-fds");
		vo2.setToCity("CAN");
		vo2.setToCityName("广州");
		l1.add(vo2);
		l2.add(vo2);
		l3.add(vo2);
		l4.add(vo2);
		map.put("1", l1);
		map.put("2", l2);
		map.put("3", l3);
		map.put("4", l4);
		
		ExcelNoFlightInfo.noInfoExcel(map,"test","C:/");
	}
}
